package com.erhuogo.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erhuogo.bean.FileBean;
import com.erhuogo.bean.Response;
import com.erhuogo.enumerate.Result;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
	
	private static final long serialVersionUID = -8374164864418347451L;
	private static Logger logger = LoggerFactory.getLogger(FileUploadAction.class);
	
	private File upload;//File类型属性xxx,存放文件内容
	private String uploadFileName;//xxxFileName,上传文件的名称
	private String uploadContentType;//xxxContentType,上传文件的文件类型
	
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	// 返回页面数据
	private Response response = new Response();

	public Response getResponse() {
		return response;
	}

	public void setResponse(Response response) {
		this.response = response;
	}
    
	@Override
    public String execute() throws Exception{
		// 将上传文件 保存到服务器端
		// 源文件 upload
		// 目标文件
		File destFile = new File(ServletActionContext.getServletContext().getRealPath("/img/upload") + File.separator + uploadFileName);
		logger.info("destFile Path:" + destFile.getPath());
		logger.info("destFile AbsolutePath:" + destFile.getAbsolutePath());
		logger.info("destFile Name:" + destFile.getName());
		// 文件复制
		FileUtils.copyFile(upload, destFile);
		response.setMessage("文件上传成功");
		response.setResult(Result.SUCCESS.toString());
		FileBean showFile = new FileBean();
		showFile.setFileName(destFile.getName());
		showFile.setFilePath("img"+ File.separator + "upload" + File.separator + destFile.getName());
		response.setData(showFile);
		return "uploadSuccess";
    }

	

}
